package com.y.architecture.location;

import android.content.Context;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationListener;

import rx.Observable;
import rx.Subscriber;

/**
 * Created by youjingjing on 2017/11/22.
 */

public class LocationLateKnownOnSubscribe implements Observable.OnSubscribe<AMapLocation>{
    private final Context context;

    public LocationLateKnownOnSubscribe(Context context) {
        this.context = context;
    }

    @Override
    public void call(final Subscriber<? super AMapLocation> subscriber) {
        AMapLocation lateKnownLocation = LocationClient.get(context).getLateKnownLocation();
        if (LocationUtil.isLocationResultEffective(lateKnownLocation)){
            subscriber.onNext(lateKnownLocation);
            subscriber.onCompleted();
        }else{
            AMapLocationListener amapListener = new AMapLocationListener() {
                @Override
                public void onLocationChanged(AMapLocation aMapLocation) {
                    subscriber.onNext(aMapLocation);
                    subscriber.onCompleted();
                }
            };
            LocationClient.get(context).locate(amapListener);
        }
    }
}
